package com.baosf.serviceorder.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baosf.model.order.Order;
import com.baosf.model.product.Product;
import com.baosf.serviceorder.service.OrderService;
import com.baosf.serviceorder.feign.ProductFeignClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
//    服务发现
    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private LoadBalancerClient loadBalancerClient;
    @Resource
    private ProductFeignClient productFeignClient;
    @Override
    @SentinelResource(value = "createOrder")
    public Order createOrder(Long userId, Long productId) {
//        Product product = getProductRemote(productId);
//        Product product = getProductRemoteLoadBalance(productId);
//        Product product = getProductRemoteLoadBalanceAnnotation(productId);
        Product product = getProductRemoteOpenFeign(productId);

        Order order = new Order();
        order.setId(0L);
        order.setTotalAmount(product.getPrice().multiply(BigDecimal.valueOf(product.getNum())));
        order.setUserId(userId);
        order.setNickName("greenbook");
        order.setAddress("中关村大街10号院");
        order.setProducts(List.of(product));
        return order;
    }
    private Product getProductRemote(Long productId) {
        // 1.获取商品服务的 IP + Port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance instance = instances.get(0);

        // 2.获取商品服务的 URL: "http://localhost:8081/product/1"
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        System.out.println("远程调用：" + url);
        // 3.调用商品服务的接口，获取商品信息
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }

    /**
     * LoadBalance 注解是使用负载均衡更简便的一种方式，只需要在我们注入的 RestTemplate Bean 添加该注解即可使用
     * @param productId
     * @return
     */
    private Product getProductRemoteLoadBalance(Long productId) {
        // 1.通过负载均衡获取商品服务的 IP + Port
        ServiceInstance instance = loadBalancerClient.choose("service-product");

        // 2.获取商品服务的 URL: "http://localhost:8081/product/1"
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        System.out.println("远程调用：" + url);
        // 3.调用商品服务的接口，获取商品信息
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }
    /**
     * 版本3: 注解式负载均衡
     * 无需再通过 loadBalancerClient 去获取微服务实例，这里只需要将 URL 指定为微服务名称即可。
     * @param productId
     * @return
     */
    private Product getProductRemoteLoadBalanceAnnotation(Long productId) {
        // 1.获取商品服务的 URL: "http://localhost:8081/product/1"
        String url = "http://service-product/product/" + productId;

        // 2.调用商品服务的接口，获取商品信息
        Product product = restTemplate.getForObject(url, Product.class);

        return product;
    }
    // 版本4: 注解式负载均衡 + FeignClient
    private Product getProductRemoteOpenFeign (Long productId) {
        // 1.通过负载均衡获取商品服务的 IP + Port
        Product product = productFeignClient.getProduct(productId);
        // 2.返回商品信息
        return product;
    }
}
